/**
 * 创建字典表
 */
module.exports = {
  up: async (queryInterface, Sequelize) => {
    // 创建字典类型表
    await queryInterface.createTable('dictionary_types', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      tenant_id: {
        type: Sequelize.UUID,
        allowNull: true,
        comment: '租户ID，为null表示系统字典',
      },
      code: {
        type: Sequelize.STRING(100),
        allowNull: false,
        unique: true,
        comment: '字典编码（唯一标识）',
      },
      name: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '字典名称',
      },
      english: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '英文名称',
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '描述',
      },
      status: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '状态（启用/禁用）',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
      },
    });

    // 创建字典项表
    await queryInterface.createTable('dictionary_items', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      type_id: {
        type: Sequelize.UUID,
        allowNull: false,
        comment: '字典类型ID',
        references: {
          model: 'dictionary_types',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      label: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '字典标签（显示值）',
      },
      english: {
        type: Sequelize.STRING(100),
        allowNull: true,
        comment: '英文标签',
      },
      value: {
        type: Sequelize.STRING(100),
        allowNull: false,
        comment: '字典值（实际值）',
      },
      order_num: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '排序序号',
      },
      status: {
        type: Sequelize.BOOLEAN,
        allowNull: false,
        defaultValue: true,
        comment: '状态（启用/禁用）',
      },
      description: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '描述',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP'),
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.literal('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'),
      },
    });

    // 添加索引
    await queryInterface.addIndex('dictionary_types', ['tenant_id']);
    await queryInterface.addIndex('dictionary_types', ['code']);
    await queryInterface.addIndex('dictionary_items', ['type_id']);
    await queryInterface.addIndex('dictionary_items', ['value']);
    await queryInterface.addIndex('dictionary_items', ['order_num']);
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('dictionary_items');
    await queryInterface.dropTable('dictionary_types');
  },
};


